package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.SysPtClassExpAddBo;
import com.ruoyi.system.bo.SysPtClassExpQueryBo;
import com.ruoyi.system.bo.SysPtClassExpEditBo;
import com.ruoyi.system.domain.SysPtClassExp;
import com.ruoyi.system.mapper.SysPtClassExpMapper;
import com.ruoyi.system.vo.SysPtClassExpVo;
import com.ruoyi.system.service.ISysPtClassExpService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 班级实验管理Service业务层处理
 *
 * @author 莫修
 * @date 2021-11-28
 */
@Service
public class SysPtClassExpServiceImpl extends ServiceImpl<SysPtClassExpMapper, SysPtClassExp> implements ISysPtClassExpService {

    @Override
    public SysPtClassExpVo queryById(Long classExpId){
        return getVoById(classExpId, SysPtClassExpVo.class);
    }

    @Override
    public TableDataInfo<SysPtClassExpVo> queryPageList(SysPtClassExpQueryBo bo) {
        PagePlus<SysPtClassExp, SysPtClassExpVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysPtClassExpVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysPtClassExpVo> queryList(SysPtClassExpQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysPtClassExpVo.class);
    }

    private LambdaQueryWrapper<SysPtClassExp> buildQueryWrapper(SysPtClassExpQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPtClassExp> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getClassId() != null, SysPtClassExp::getClassId, bo.getClassId());
        lqw.eq(bo.getExpId() != null, SysPtClassExp::getExpId, bo.getExpId());
        lqw.eq(bo.getIsEnable() != null, SysPtClassExp::getIsEnable, bo.getIsEnable());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysPtClassExpAddBo bo) {
        SysPtClassExp add = BeanUtil.toBean(bo, SysPtClassExp.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysPtClassExpEditBo bo) {
        SysPtClassExp update = BeanUtil.toBean(bo, SysPtClassExp.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysPtClassExp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
